package com.hl.util.KL.Encryption.ECC;

import java.math.BigInteger;

class BigIntegerExtend {
	Boolean IsEven(BigInteger value){
		return Mod(value, BigInteger.valueOf(2)).intValue()==0;
	}
	
	String ToBase256String(BigInteger bInt) {
		byte[] tmpByte = bInt.toByteArray();
		String str256 = "";
		for (int i = 0; i < tmpByte.length; i++) {
			str256 += (char) tmpByte[i];
		}
		return str256;
	}

	BigInteger GetModeInv(BigInteger n, BigInteger p) {
		BigInteger b1 = BigInteger.valueOf(0), b2 = BigInteger.valueOf(1), b3, q, r1, r2, r3;
		r1 = p;
		r2 = n;
		q = r1.divide(r2);
		r3 = r1.mod(r2);
		b3 = b1.subtract(q.multiply(b2));

		while (r3.compareTo(BigInteger.valueOf(1)) > 0) {
			r1 = r2;
			r2 = r3;
			b1 = b2;
			b2 = b3;
			q = r1.divide(r2);
			r3 = r1.mod(r2);
			b3 = b1.subtract(q.multiply(b2));
		}
		return b3;
	}

	String ToByteString(BigInteger bigInt) {
		String sb = "";
		byte[] bt = bigInt.toByteArray();
		String t = "";
		for (int i = 0; i < bt.length; i++) {
			t = "00000000" + Integer.toBinaryString(bt[i]);
			t = t.substring(t.length() - 8);
			sb += t;
		}
		sb = sb.substring(sb.indexOf("1"));
		return sb;
	}

	BigInteger Mod(BigInteger a, BigInteger b) {
		BigInteger tmp = a.mod(b);
		if (tmp.signum() < 0) {
			tmp = tmp.add(b);
		}
		return tmp;
	}
}
